package com.leetcode.string;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 17. 电话号码的字母组合
 * 给定一个仅包含数字2-9的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 *  1(0_0)  2(abc)  3(def)
 *  4(ghi)  5(jkl)  6(mno)
 *  7(pqrs) 8(tuv)  9(wxyz)
 * 示例 1：
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 * 示例 2：
 * 输入：digits = ""
 * 输出：[]
 * 示例 3：
 * 输入：digits = "2"
 * 输出：["a","b","c"]
 * 提示：
 * 0 <= digits.length <= 4
 * digits[i] 是范围 ['2', '9'] 的一个数字。
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @author ymy
 * @date 2021年11月22日 17:39
 */
class Code17 {
    public static void main(String[] args) {
        Code17 code06 = new Code17();
        List<String> list = code06.letterCombinations("2");
        System.out.println(list);

    }
    static Map<Integer,Character[]> map = null;
    static {
        Character[] t2 = new Character[]{'a','b','c'};
        Character[] t3 = new Character[]{'d','e','f'};
        Character[] t4 = new Character[]{'g','h','i'};
        Character[] t5 = new Character[]{'j','k','l'};
        Character[] t6 = new Character[]{'m','n','o'};
        Character[] t7 = new Character[]{'p','q','r','s'};
        Character[] t8 = new Character[]{'t','u','v'};
        Character[] t9 = new Character[]{'w','x','y','z'};
        map = new HashMap<>();
        map.put(2,t2);
        map.put(3,t3);
        map.put(4,t4);
        map.put(5,t5);
        map.put(6,t6);
        map.put(7,t7);
        map.put(8,t8);
        map.put(9,t9);
    }
    List<String> list = new ArrayList<>();
    StringBuilder builder = new StringBuilder();
    int[] nums = null;
    public List<String> letterCombinations(String digits) {
        if(digits == null || digits.length() == 0){
            return new ArrayList<String>();
        }
        nums = new int[digits.length()];
        for(int i = 0;i<digits.length();i++){
            nums[i] = Integer.valueOf(String.valueOf(digits.charAt(i)));
        }
        dfs(0);
        return list;
    }
    public void dfs(int index){
        if(index >= nums.length){
            list.add(new String(builder.toString()));
            return;
        }
        Character[] ch = map.get(nums[index]);
        for(int i = 0;i<ch.length;i++){
            builder.append(ch[i]);
            dfs(index+1);
            builder.delete(builder.length()-1,builder.length());
        }
    }
}
